package com.fotkidemo.util;

import com.fotkidemo.beans.EntryElement;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: roma
 * Date: 6/18/12
 * Time: 6:41 PM
 * To change this template use File | Settings | File Templates.
 */

/**
 * Кэш хранит кэши entry-элементов.
 * Кэши entry-элементов распределятся по id.
 */
public class EntryCaches {
    private static EntryCaches instance;

    private final Map<String, EntryElementCache<EntryElement>> cacheMap =
            Collections.synchronizedMap(new HashMap<String, EntryElementCache<EntryElement>>());

    public static void initInstance(){
        if (instance == null) {
            instance = new EntryCaches();
        }
    }

    public static EntryCaches getInstance() {
//        if (instance == null) {
//            instance = new EntryCaches();
//        }
        return instance;
    }

    private EntryCaches() {}

    public EntryElementCache<EntryElement> getCache(String id) {
        EntryElementCache<EntryElement> cache = cacheMap.get(id);
        if (cache == null) {
            cache = new EntryElementCache<EntryElement>(id);
            cacheMap.put(id, cache);
        }
        return cache;
    }

    public void clearCaches() {
        cacheMap.clear();
    }
}
